/* Use decision theoretical framework to evaluate graphical treatments based on
type I/II error rates.
*/

clear
set more off
set scheme  s1mono 

*Set working directory to rd graph folder:
cd "${main}"

cap log close
log using "${logs}/decision_theory_risks.log", replace


* i=1: Just 0 and 0.324. i=2: All data.
* When using full data, need to average carefully: want to weight all disconts
* equally, and 1.5sigma appears less often in the data.
foreach i in 1 2 {

if `i' == 1 {
	di "*******USING ONLY 0 AND 0.324sigma*******"
}
if `i' == 2 {
	di "*******USING ALL DATA*******"
}

use "${dat}/survey_merged_rd_estimates_micro.dta", clear

gen confident = playerchosenStakes == 1

sort phase playertreatment_group playerdgp abs_disc

drop disc
gen disc = abs_disc

if `i' == 1 {
	keep if abs_disc == 0 | (abs_disc > 0.323 & abs_disc < 0.325)
}
replace disc = disc > 0

* Andrews and Shapiro risk
gen q = (0 + 0.33) / 2
replace q = (0.33 + 0.5) / 2 if y == 0 & confident == 0
replace q = (0.5 + 0.67) / 2 if y == 1 & confident == 0
replace q = (0.67 + 1) / 2 if y == 1 & confident == 1
gen as_risk = 0.5 - abs(q - 0.5)

tempfile d
save "`d'", replace

if `i' == 2 {
collapse (mean) y (first) disc, by(phase playertreatment_group playerdgp abs_disc)
}

collapse (mean) y, by(phase playertreatment_group playerdgp disc)

reshape wide y, i(playerdgp phase playertreatment_group) j(disc)

* y0 is the type I error rate
* 1 - y1 is the type II error rate
replace y1 = 1 - y1

rename y0 t1
rename y1 t2

bysort phase playertreatment_group: egen bayes_t1 = mean(t1)
bysort phase playertreatment_group: egen bayes_t2 = mean(t2)

* Bayes
preserve
keep if playerdgp == "BDGP2"
drop t* playerdgp
gen risk1 = bayes_t1 * 1 + bayes_t2 * 1
gen risk4 = bayes_t1 * 4 + bayes_t2 * 1
di "Bayes risks"
list
* Now determine when treatment A is preferred
gen t1_a_temp = bayes_t1 if playertreatment_group == "A"
gen t2_a_temp = bayes_t2 if playertreatment_group == "A"
bysort phase: egen bayes_t1_a = max(t1_a_temp)
bysort phase: egen bayes_t2_a = max(t2_a_temp)
keep phase playertreatment_group bayes_t1 bayes_t1_a bayes_t2 bayes_t2_a
drop if playertreatment_group == "A"
gen crit_val = (bayes_t2 - bayes_t2_a) / (bayes_t1_a - bayes_t1)
replace crit_val = . if crit_val < 0
keep playertreatment_group phase crit_val
di "Bayes risks"
list
restore

* Minimax
preserve
bysort phase playertreatment_group: egen risk1 = max(t1 * 1 + t2 * 1)
bysort phase playertreatment_group: egen risk4 = max(t1 * 4 + t2 * 1)
bysort phase playertreatment_group: egen risk10 = max(t1 * 10 + t2 * 1)
bysort phase playertreatment_group: egen risk100 = max(t1 * 100 + t2 * 1)
bysort phase  playertreatment_group: egen risk1000 = max(t1 * 1000 + t2 * 1)
* Pick any DGP to keep to avoid duplicate observations
keep if playerdgp == "BDGP2"
drop t* playerdgp bayes*
di "Minimax risks"
list
restore

* Andrews and Shapiro
use "`d'", clear

collapse (mean) as_risk, by(phase playertreatment_group playerdgp disc)
reshape wide as_risk, i(playerdgp phase playertreatment_group) j(disc)
bysort phase playertreatment_group: egen mean_as_risk0 = mean(as_risk0)
bysort phase playertreatment_group: egen mean_as_risk1 = mean(as_risk1)
bysort phase playertreatment_group: egen as_risk = mean(as_risk0 + as_risk1)
bysort phase playertreatment_group: egen as_risk4 = mean(4*as_risk0 + as_risk1)

preserve
keep if playerdgp == "BDGP2"
drop playerdgp as_risk0 as_risk1
di "Andrews & Shapiro risks"
list
restore




********************************************************************************
* Regressions and the Bugni et al. program to test for statistical significance*	
********************************************************************************

* Bugni et al. program
use "`d'", clear
foreach p in 1 2 3 6 {
preserve

keep if phase == `p'
sort pid playerdgp
by pid: gen pid_graph_n = _n
gen pid_zeros = disc * pid_graph_n
by pid (playerdgp): gen pid_zero_index = sum(pid_zeros)
egen pid_strata = max(pid_zero_index), by(pid)

gen playertreatment_group_num = 0 // Interpreted as control group
replace playertreatment_group_num = 2 if playertreatment_group == "B"
replace playertreatment_group_num = 3 if playertreatment_group == "C"
replace playertreatment_group_num = 4 if playertreatment_group == "D"

collapse (mean) y as_risk (first) playertreatment_group_num pid_strata, by(pid disc)
reshape wide y as_risk, i(pid) j(disc)
gen bayes_1 = 1 * y0 - 1 * y1
gen bayes_4 = 4 * y0 - 1 * y1
gen as_1 = 1 * as_risk0 + 1 * as_risk1
gen as_4 = 4 * as_risk0 + 1 * as_risk1
di "Phase: `p'"
di "Bayes Risks"
di "Kappa/Phi = 1"
car_sfe bayes_1 playertreatment_group_num, strata(pid_strata)
di "Kappa/Phi = 4"
car_sfe bayes_4 playertreatment_group_num, strata(pid_strata)
di "Andrews and Shapiro Risks"
di "Kappa/Phi = 1"
car_sfe as_1 playertreatment_group_num, strata(pid_strata)
di "Kappa/Phi = 4"
car_sfe as_4 playertreatment_group_num, strata(pid_strata)

restore
}

}

cap log close
